package com.jzqf.harmonydemo.component;

import com.jzqf.harmonydemo.utils.LogUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 自定义View
 *
 * @author LiuWeiHao
 * 2021/5/21 9:17
 */
public class CircleView extends Component implements Component.EstimateSizeListener, Component.DrawTask, Component.TouchEventListener {
    private final String TAG = CircleView.class.getSimpleName();
    private Paint paint;

    public CircleView(Context context) {
        super(context);
        init(context);
    }

    public CircleView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    public CircleView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    public CircleView(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init(context);
    }

    public void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(20f);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        //测量大小
        setEstimateSizeListener(this);
        //添加绘制任务
        addDrawTask(this);
        //添加监听
        setTouchEventListener(this);
    }

    @Override
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        int width = Component.EstimateSpec.getSize(widthEstimateConfig);
        int height = Component.EstimateSpec.getSize(heightEstimateConfig);
        /*
         * UNCONSTRAINT 父组件对子组件没有约束，表示子组件可以任意大小。
         * PRECISE  父组件已确定子组件的大小。
         * NOT_EXCEED 已为子组件确定了最大大小，子组件不能超过指定大小。
         */
        setEstimatedSize(
                Component.EstimateSpec.getChildSizeWithMode(width, widthEstimateConfig, EstimateSpec.NOT_EXCEED),
                Component.EstimateSpec.getChildSizeWithMode(height, heightEstimateConfig, EstimateSpec.NOT_EXCEED)
        );
        return true;//返回true测量值生效
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.drawRect(new Rect(50, 50, 300, 300), paint);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        LogUtil.d(TAG, "action=" + touchEvent.getAction());
        //TODO action只有按下，没有抬起输出
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                paint.setColor(Color.RED);
                invalidate();
                break;
            case TouchEvent.POINT_MOVE:
                this.getContentPositionX();
                MmiPoint absPoint = touchEvent.getPointerPosition(touchEvent.getIndex());
                Point point = new Point(absPoint.getX() - getContentPositionX(),
                        absPoint.getY() - getContentPositionY());
                //TODO 顺手势移动的痕迹
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                paint.setColor(Color.GREEN);
                invalidate();
                break;

        }
        return false;
    }
}
